Page({
  data: {
    fromAPI: '', 
    params: '', 
    currentPath: '', 
    theme: 'light',
    status: '', 
    message: '', 
    detailInfo: '',
    weburl:'',
    formData: {},
    result: {},
    schema: {
      items: []
    },
    showCallbackInfo: false,
    callbackType: '',
    callbackMessage: ''
  },
  // 表单数据变化回调
  getFormData(e) {
    const { formData } = e.detail;
    this.setData({
      formData: formData,
      showCallbackInfo: true,
      callbackMessage: '表单数据已更新',
      callbackType: 'success'
    });
  },
  showApiResult(result) {
    const isSuccess = !result || !result.error && (result.success !== false);
    
    this.setData({
      result: result,
      showCallbackInfo: true,
      callbackMessage: isSuccess ? 'API调用成功' : 'API调用失败',
      callbackType: isSuccess ? 'success' : 'fail'
    });
  },
  showCallbackInfo(type, message) {
    this.setData({
      showCallbackInfo: true,
      callbackType: type,
      callbackMessage: message
    });
  },
  openDocFunc() {
      wx.navigateTo({
        url: '/page/webview/webview?src='+this.data.weburl,
      })
    },
  onShareAppMessage() {
    return {
      title: '目标页面',
      path: 'packageAPI/pages/page/navigator/targetPage'
    }
  },

  onLoad(options) {
    
    const fromAPI = options.from || 'wx.navigateTo';
    const params = JSON.stringify(options);
    const currentPath = this.route || 'unknown';
    
    const status = options.status || '';
    const message = options.message || '';
    const weburl = options.weburl || '';
    
    const detailedNavInfo = {
      errMsg: "navigateTo:ok",
      eventChannel: "[Object]",
      timestamp: new Date().getTime(),
      fromAPI: fromAPI,
      currentPath: currentPath,
      params: options
    };
    
    const defaultResult = {
      "navigateToInfo": detailedNavInfo
    };
    
    this.setData({
      fromAPI: fromAPI,
      params: params,
      currentPath: currentPath,
      theme: wx.getSystemInfoSync().theme || 'light',
      status: status,
      message: message,
      weburl: weburl,
      detailInfo: JSON.stringify(detailedNavInfo, null, 2),
      schema: {
        items: []
      },
      formData: {},
      result: defaultResult
    });
    
    console.log('targetPage data set completed:', this.data);
    
    this.showApiResult({
      success: true,
      message: '目标页面加载成功',
      fromAPI: fromAPI,
      params: options
    });
    
    
    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme});
      });
    }

    try {
      const eventChannel = this.getOpenerEventChannel();
      if (eventChannel && typeof eventChannel.emit === 'function') {
        setTimeout(() => {
          try {
            eventChannel.emit('onMessageFromTargetPage', {
              data: '来自目标页面的回复消息',
              timestamp: new Date().getTime(),
              pageInfo: {
                path: this.data.currentPath,
                params: this.data.params
              }
            });
            console.log('已向导航页面发送消息');
          } catch (emitError) {
            console.error('发送消息失败:', emitError);
          }
        }, 1000);
      } else {
        console.warn('EventChannel对象不包含emit方法或未获取到EventChannel');
      }
    } catch (error) {
      console.error('获取EventChannel失败:', error);
    }
  },

  navigateBack() {
    try {
      try {
        const eventChannel = this.getOpenerEventChannel();
        if (eventChannel && typeof eventChannel.emit === 'function') {
          // 返回前向导航页面发送消息
          eventChannel.emit('onMessageFromTargetPage', {
            data: '目标页面即将返回',
            action: 'navigateBack',
            timestamp: new Date().getTime()
          });
          console.log('已向导航页面发送返回消息');
        } else {
          console.warn('EventChannel对象不包含emit方法或未获取到EventChannel');
        }
      } catch (eventError) {
        console.error('EventChannel操作失败:', eventError);
      }
      
      wx.navigateBack({
        delta: 1,
        success: (res) => {
          console.log('返回成功', res);
          this.showApiResult({
            success: true,
            message: '返回上一页成功',
            result: res
          });
        },
        fail: (error) => {
          console.error('返回失败', error);
          this.showApiResult({
            success: false,
            error: error.message
          });
        }
      });
    } catch (error) {
      console.error('navigateBack执行异常', error);
      this.showApiResult({
        success: false,
        error: error.message
      });
    }
  },
  
  navigateToHome() {
    try {
      wx.navigateTo({
        url: './navigator',
        success: (res) => {
          console.log('返回首页成功', res);
          this.showApiResult({
            success: true,
            message: '返回导航首页成功',
            result: res
          });
        },
        fail: (error) => {
          console.error('返回首页失败', error);
          this.showApiResult({
            success: false,
            error: error.message
          });
        }
      });
    } catch (error) {
      console.error('navigateToHome执行异常', error);
      this.showApiResult({
        success: false,
        error: error.message
      });
    }
  }
});